package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.service.LocalSynService;
import com.mission.schedule.service.ServerDataDownLocalService;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * 数据同步下载对话框(登录，数据修复使用)
 * Created by liwei on 2017/4/18.
 */

public class ServerDataDownLocalServiceDialog extends Dialog {

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    private ProgressBar initialize_pr, downperson_pr, downcalender_pr, setcalender_pr;
    private ImageView initialize_im, downperson_im, downcalender_im, setcalender_im;
    //0 初始状态 ,1 成功 ,-1 失败
    private int initialize = 0, downperson = 0, downcalender = 0, setcalender = 0;
    ServerDataDownLocalReceiver serverDataDownLocalReceiver;

    public ServerDataDownLocalServiceDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;


        setCancelable(false);

        View view = LayoutInflater.from(context).inflate(R.layout.server_down_local_dialog, null);
        setContentView(view);


        initialize_pr = (ProgressBar) view.findViewById(R.id.initialize_pr);
        initialize_pr.setVisibility(View.VISIBLE);
        downperson_pr = (ProgressBar) view.findViewById(R.id.downperson_pr);
        downcalender_pr = (ProgressBar) view.findViewById(R.id.downcalender_pr);
        setcalender_pr = (ProgressBar) view.findViewById(R.id.setcalender_pr);

        initialize_im = (ImageView) view.findViewById(R.id.initialize_im);
        downperson_im = (ImageView) view.findViewById(R.id.downperson_im);
        downcalender_im = (ImageView) view.findViewById(R.id.downcalender_im);
        setcalender_im = (ImageView) view.findViewById(R.id.setcalender_im);


        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        serverDataDownLocalReceiver = new ServerDataDownLocalReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("ServerDataDownLocalReceiver");
        context.registerReceiver(serverDataDownLocalReceiver, intentFilter);


        Intent LocalSyni = new Intent(context, ServerDataDownLocalService.class);
        LocalSyni.setAction("ServerDataDownLocalService");
        LocalSyni.setPackage(context.getPackageName());
        context.startService(LocalSyni);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.alpha = 0.52f;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);// 设置生效
        window.setGravity(Gravity.CENTER);
    }

    class ServerDataDownLocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                if (intent.getStringExtra("downCalender") != null) {
                    initialize = -1;
                    if (intent.getStringExtra("downCalender").equals("downCalender")) {
                        initialize = 1;
                        initialize_pr.setVisibility(View.GONE);
                        initialize_im.setVisibility(View.VISIBLE);
                    } else {
                        initialize = -1;
                        initialize_pr.setVisibility(View.GONE);
                        initialize_im.setVisibility(View.VISIBLE);
                    }
                    downperson_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("downRepeat") != null) {
                    initialize_pr.setVisibility(View.GONE);
                    initialize_im.setVisibility(View.VISIBLE);
                    if (initialize != 1) {
                        initialize = -1;
                    }
                    if (intent.getStringExtra("downRepeat").equals("downRepeat")) {
                        downperson = 1;
                        downperson_pr.setVisibility(View.GONE);
                        downperson_im.setVisibility(View.VISIBLE);
                    } else {
                        downperson = -1;
                        downperson_pr.setVisibility(View.GONE);
                        downperson_im.setVisibility(View.VISIBLE);
                    }
                    downcalender_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("downNote") != null) {
                    downperson_pr.setVisibility(View.GONE);
                    downperson_im.setVisibility(View.VISIBLE);
                    if (downperson != 1) {
                        downperson = -1;
                    }
                    if (intent.getStringExtra("downNote").equals("downNote")) {
                        downcalender = 1;
                        downcalender_pr.setVisibility(View.GONE);
                        downcalender_im.setVisibility(View.VISIBLE);
                    } else {
                        downcalender = -1;
                        downcalender_pr.setVisibility(View.GONE);
                        downcalender_im.setVisibility(View.VISIBLE);
                    }
                    setcalender_pr.setVisibility(View.VISIBLE);
                } else if (intent.getStringExtra("downPerson") != null) {
                    downcalender_pr.setVisibility(View.GONE);
                    downcalender_im.setVisibility(View.VISIBLE);
                    if (downcalender != 1) {
                        downcalender = -1;
                    }
                    if (intent.getStringExtra("downPerson").equals("downPerson")) {
                        setcalender = 1;
                        setcalender_pr.setVisibility(View.GONE);
                        setcalender_im.setVisibility(View.VISIBLE);
                    } else {
                        setcalender = -1;
                        setcalender_pr.setVisibility(View.GONE);
                        setcalender_im.setVisibility(View.VISIBLE);
                    }
                    if (isShowing()) {
                        dismissDialog();
                    }
                }
                if (initialize == -1) {
                    initialize_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (downperson == -1) {
                    downperson_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (downcalender == -1) {
                    downcalender_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                } else if (setcalender == -1) {
                    setcalender_im.setBackground(context.getResources().getDrawable(R.mipmap.btn_cuo));
                }
            } else {
                dismissDialog();
            }
        }

    }

    private void dismissDialog(){
        dismiss();
        if (serverDataDownLocalReceiver != null){
            context.unregisterReceiver(serverDataDownLocalReceiver);
        }
        Toast.makeText(context,"数据下载完成",Toast.LENGTH_SHORT).show();
    }

}
